-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (32 loc) · 854 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
using namespace std;
void findPairs(vector<int>& arr, int target) {
bool found = false;
cout << "Pairs with sum " << target << ":\n";
for (int i = 0; i < arr.size() - 1; i++) {
for (int j = i + 1; j < arr.size(); j++) {
if (arr[i] + arr[j] == target) {
cout << "(" << arr[i] << ", " << arr[j] << ")\n";
found = true;
}
}
}
if (!found) {
cout << "No pairs found.\n";
}
}
int main() {
int n, target;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter " << n << " elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the target sum: ";
cin >> target;
findPairs(arr, target);
return 0;
}